package com.mgetool.ui;

import java.util.Vector;

import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.GC;
/**
 * 绘制组件的容器类，可以添加多个组件，统一管理
 * @author wangbin
 *
 */
public abstract class DrawableContainer extends AbstractDrawable{
	/**
	 * 存放组件的容器
	 */
	protected Vector<DrawableInterface> childs = new Vector<DrawableInterface>();
	/**
	 * 绘制所有组件
	 * @param canvas
	 * @param paint
	 */
	protected void onDrawChilds(GC gc){
		synchronized(childs){
			for(int i = 0; i < childs.size(); i++){
				childs.elementAt(i).onDraw(gc);
			}
		}
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		for(int i = 0; i < childs.size(); i++){
			childs.elementAt(i).onDestroy();
		}
	}

	@Override
	public void onDraw(GC gc) {
		// TODO Auto-generated method stub
		onDrawChilds(gc);
	}

	@Override
	public boolean onKey(int keyCode, KeyEvent keyEvent) {
		// TODO Auto-generated method stub
		return onKeyChilds(keyCode, keyEvent);
	}

	@Override
	public boolean onMouseEvent(int state, MouseEvent event) {
		// TODO Auto-generated method stub
		return onTouchChilds(state, event);
	}

	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		onUpdataChilds();
	}

	/**
	 * 更新所有组件
	 */
	public void onUpdataChilds(){
		for(int i = 0; i < childs.size(); i++){
			childs.elementAt(i).onUpdate();
		}
	}
	
	/**
	 * 添加一个可绘制组件
	 * @param drawable
	 */
	protected void addChild(DrawableInterface drawable){
		childs.addElement(drawable);
	}
	
	/**
	 * 删除某个可绘制组件
	 * @param drawableInterface
	 */
	protected void remove(DrawableInterface drawableInterface){
		childs.remove(drawableInterface);
	}
	
	/**
	 * 移除所有的组件
	 */
	protected void removeAllChilds(){
		for(int i = 0; i < childs.size(); i++){
			childs.elementAt(i).onDestroy();
		}
		childs.removeAllElements();
	}
	
	/**
	 * 获取被点击的组件索引
	 * @param x
	 * @param y
	 * @return
	 */
	public int getClickedChildIdx(int x, int y){
		for(int i = 0; i < childs.size(); i++){
			boolean isUsed = childs.elementAt(i).isClicked(x, y);
			if(isUsed){
				return i;
			}
		}
		return -1;
	}
	
	/**
	 * 将某个组件提至顶部
	 * @param drawable
	 */
	public void upChild2Top(DrawableInterface drawable){
		if(childs.contains(drawable)){
			childs.removeElement(drawable);
			childs.addElement(drawable);
		}
	}
	
	
	/**
	 * 获取指定索引的组件
	 * @param idx
	 * @return
	 */
	protected DrawableInterface getCihld(int idx){
		if(idx > -1 && idx < childs.size()){
			return childs.elementAt(idx);
		}
		return null;
	}
	
	public boolean onKeyChilds(int state, KeyEvent keyEvent){
		for(int i = 0; i < childs.size(); i++){
			boolean isUsed = childs.elementAt(i).onKey(state, keyEvent);
			if(isUsed){
				return true;
			}
		}
		return false;
	}

	public boolean onTouchChilds(int state, MouseEvent event){
		for(int i = 0; i < childs.size(); i++){
			boolean isUsed = childs.elementAt(i).onMouseEvent(state, event);
			if(isUsed){
				return true;
			}
		}
		return false;
	}

	public boolean isClicked(int x, int y){
		if(getClickedChildIdx(x, y) != -1){
			return true;
		}
		return false;
	}
	
}
